package cn.net.shuxing.yifu.common.back.controller;

import cn.net.shuxing.yifu.common.bean.CashWallet;
import cn.net.shuxing.yifu.common.bean.CompanyBankCard;
import cn.net.shuxing.yifu.common.bean.RechargeHistory;
import cn.net.shuxing.yifu.common.constant.CashChangeType;
import cn.net.shuxing.yifu.common.constant.DealType;
import cn.net.shuxing.yifu.common.constant.PaymentStatus;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.dto.RechargeHistoryDto;
import cn.net.shuxing.yifu.common.service.CashWalletService;
import cn.net.shuxing.yifu.common.service.CompanyBankCardService;
import cn.net.shuxing.yifu.common.service.RechargeHistoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
public class OfflineRechargeBackController {

    @Autowired
    private CompanyBankCardService companyBankCardService;

    @Autowired
    private RechargeHistoryService rechargeHistoryService;

    @Autowired
    private CashWalletService cashWalletService;

    @RequestMapping("/back/company/bankcard/add")
    public void addCompanyBankCard(String openBank, String bankCardNum, String holderName, HttpServletResponse response) throws IOException {
        try {
            companyBankCardService.insert(openBank, bankCardNum, holderName);
            response.sendRedirect("listpage");
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("listpage");
        }
    }

    @RequestMapping("/back/company/bankcard/setDefault")
    public BaseDto setDefaultBankCard(Integer bankCardId) {
        if (companyBankCardService.setDefaultBankCard(bankCardId))
            return BaseDto.ok("success");
        return BaseDto.error("此银行卡不存在");
    }

    @RequestMapping("/back/company/bankcard/list")
    public BaseDto getCompanyBankCardList() {
        List<CompanyBankCard> allWithoutComplete = companyBankCardService.getAllWithoutComplete();
        return BaseDto.ok(allWithoutComplete);
    }

    @RequestMapping("/back/company/bankcard/del")
    public BaseDto deleteCompanyBankCard(Integer bankCardId) {
        companyBankCardService.delete(bankCardId);
        return BaseDto.ok("删除完成");
    }

    @RequestMapping("/back/recharge/offline/all")
    public BaseDto getAllOfflineRecharge(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                                         @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        Page<RechargeHistory> page = PageHelper.startPage(pageNum, pageSize);
        List<RechargeHistory> rechargeHistories = rechargeHistoryService.getsByType(1);
        List<RechargeHistoryDto> rechargeHistoryDtos = new ArrayList<>();
        for (RechargeHistory rechargeHistory : rechargeHistories) {
            rechargeHistoryDtos.add(RechargeHistoryDto.fromRechargeHistory(rechargeHistory));
        }
        PageInfo<RechargeHistoryDto> pageInfo = new PageInfo<>(rechargeHistoryDtos);
        //pageInfo.setTotal(page.getTotal());
        return BaseDto.ok(pageInfo);
    }

    @RequestMapping("/back/recharge/offline/complete")
    public BaseDto setRechargeComplete(Integer rechargeId) {
        RechargeHistory rechargeHistory = rechargeHistoryService.getByPrimaryKeyWithoutComplete(rechargeId);
        rechargeHistory.setPaymentStatus(PaymentStatus.PAID.name());
        rechargeHistoryService.update(rechargeHistory);
        cashWalletService.addCash(rechargeHistory.getUserId(), new BigDecimal(rechargeHistory.getFee()), CashChangeType.CASH_RECHAREGE, rechargeHistory.getId().toString());
        return BaseDto.ok("设置完成");
    }

    @RequestMapping("/back/recharge/offline/fail")
    public BaseDto setRechargeFail(Integer rechargeId) {
        RechargeHistory rechargeHistory = rechargeHistoryService.getByPrimaryKeyWithoutComplete(rechargeId);
        rechargeHistory.setPaymentStatus(PaymentStatus.PAY_FAIL.name());
        rechargeHistoryService.update(rechargeHistory);
        return BaseDto.ok("设置完成");
    }
}
